package sk.tuke.constraintbuilder.editor.providers;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import sk.tuke.constraintbuilder.model.Constraint;
import sk.tuke.constraintbuilder.model.INodeElement;
import sk.tuke.constraintbuilder.model.RuleElement;
import sk.tuke.constraintbuilder.model.Model;
import sk.tuke.constraintbuilder.model.RootElement;
import sk.tuke.constraintbuilder.model.StructuredRuleElement;
import sk.tuke.constraintbuilder.model.TopElement;

public class ConstraintTreeContentProvider implements ITreeContentProvider {
	
	private RootElement constraint;

	@Override
	public void dispose() {	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if(newInput instanceof Constraint)
			this.constraint = (Constraint) newInput;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		Object[] elements = null;
		if(inputElement instanceof Constraint){
			elements = new Object[] { ((Constraint) constraint).getConditions(), ((Constraint) constraint).getRules()};
		}	
		return elements;
	}

	@Override
	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof TopElement){
			TopElement topElement = (TopElement) parentElement;
			return topElement.getElements().toArray();
		}
		else if (parentElement instanceof StructuredRuleElement) {
			StructuredRuleElement ruleElement = (StructuredRuleElement) parentElement;
			Object[] children = new Object[] {ruleElement.getElement()};
			return children;
		}
		else if (parentElement instanceof INodeElement) {
			INodeElement nodeElement = (INodeElement) parentElement;
			return nodeElement.getElements().toArray();
		}
		
//		if(parentElement instanceof INode){
//			INode nodeElement = (INode) parentElement;
//			for (Element element : nodeElement.getElements()) {
//				element.setParent((INode) parentElement);
//			}
//			return nodeElement.getElements().toArray();
//		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		return null;

	}

	@Override
	public boolean hasChildren(Object element) {
		if(element instanceof INodeElement || 
		   element instanceof StructuredRuleElement  || 
		   element instanceof TopElement){
			return true;
		}
		return false;
	}

}
